<?php

class AuthController extends Zend_Controller_Action {

    private $_auth;

    public function init() {
        $this->_auth = Zend_Auth::getInstance();
    }

    public function loginAction() {

        $users = new Default_Model_DbTable_Gebruiker();
        $form = $this->_helper->form('login');
        $this->view->form = $form;

        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())) {
                $data = $form->getValues();
                $auth = Zend_Auth::getInstance();
                $authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(),'gebruikers');
                $authAdapter->setIdentityColumn('username')
                        ->setCredentialColumn('password')
                        ->setCredentialTreatment('MD5(?)');
                $authAdapter->setIdentity($data['username'])
                        ->setCredential($data['password']);
                $result = $auth->authenticate($authAdapter);
                if($result->isValid()) {
                    $storage = new Zend_Auth_Storage_Session();
                    $storage->write($authAdapter->getResultRowObject());
                    return $this->_helper->redirector('index', 'index');
                } else {
                    $this->view->errorMessage = "Invalid username or password. Please try again.";
                }
            }
        }
    }

    public function logoutAction() {
        $storage = new Zend_Auth_Storage_Session();
        $storage->clear();

        $answers = new Zend_Session_Namespace('answers', true);

        $answers->answer = new stdClass();

        return $this->_redirect('/auth/login');

    }

    public function lostAction() {
        $form = $this->_helper->form('lost');

        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();

                $gebruiker = new Default_Model_DbTable_Gebruiker();

                $where = $gebruiker->select()
                        ->where('username = ?', $values['username'])
                        ->where('email = ?', $values['email']);

                $result = $gebruiker->fetchAll($where);

                if($result->count()) {

                    $password = Tc_Password::generate();

                    $gebruikerData = $result->toArray();
                    $gebruikerData = $gebruikerData[0];

                    $data = array(
                            'password' => md5($password)
                    );

                    $gebruiker->update($data, $gebruiker->getAdapter()->quoteInto('id = ?', $gebruikerData['id']));

                    $transport = new Zend_Mail_Transport_Smtp('localhost');

                    $mail = new Zend_Mail();
                    $mail->addTo($values['email']);
                    $mail->setFrom('bla@bla');
                    $mail->setSubject('Verloren wachtwoord');
                    $mail->setBodyText('Je wachtwoord is veranderd in "'.$password.'"');
                    $mail->send($transport);
                }
            }
        }
        $this->view->form = $form;
    }


}



